package com.atguigu.meinian.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.meinian.constant.MessageConstant;
import com.atguigu.meinian.pojo.Address;
import com.atguigu.meinian.service.AddressService;
import com.atguigu.meinian.vo.PageResult;
import com.atguigu.meinian.vo.QueryPageBean;
import com.atguigu.meinian.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zbystart
 * @create 2021-06-29 18:13
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;


    @GetMapping("/findAllMaps")
    public Map<String,List<Map<String,String>>> findAllMaps() {

        HashMap<String, List<Map<String, String>>> data = new HashMap<>();
        ArrayList<Map<String, String>> coordinates = new ArrayList<>();
        ArrayList<Map<String, String>> addressNames = new ArrayList<>();
        data.put("gridnMaps",coordinates);
        data.put("nameMaps",addressNames);

        List<Address> list = addressService.findAllMaps();
        for (Address address : list) {
            HashMap<String, String> coordinate = new HashMap<>();
            HashMap<String, String> addressName = new HashMap<>();
            coordinate.put("lat",address.getLat());
            coordinate.put("lng",address.getLng());
            addressName.put("addressName",address.getAddressName());

            coordinates.add(coordinate);
            addressNames.add(addressName);
        }

        return data;
    }

    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return addressService.findPage(queryPageBean);
    }

    @PostMapping("/addAddress")
    public R addAddress(@RequestBody Address address) {
        addressService.addAddress(address);
        return R.ok("新增地址成功",null);
    }

    @GetMapping("/deleteById")
    public R deleteById(Integer id) {
        addressService.deleteAddressById(id);
        return R.ok("删除地址成功",null);
    }
    
}
